package com.acegear.horizon.domain.events.handlers;

import com.acegear.horizon.domain.events.RecommendCreateEvent;
import com.acegear.horizon.domain.repository.jpa.RecommendRepository;
import com.acegear.horizon.vendor.ons.ONSConsumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;

/**
 * Created by guoweike on 17/3/1.
 */
@Component
public class RecommendHandler {
    @Autowired
    private RecommendRepository recommendRepository;

    @Autowired
    private ONSConsumer consumer;

    @PostConstruct
    public void recommendCreateEventONS() {
        consumer.consume(RecommendCreateEvent.class, this::recommendCreateEvent);
    }

    @Transactional
    public void recommendCreateEvent(RecommendCreateEvent recommendCreateEvent) {
        recommendRepository.deleteByType(recommendCreateEvent.getType());
        recommendRepository.save(recommendCreateEvent.getBody());
    }
}
